﻿using System.Web.Mvc;
using System.Web.Security;
using DddCms.Application;
using DddCms.Presentation.Areas.DddCms.Utility;
using Microsoft.Practices.Unity;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using DddCms.Domain.Utility;
using DddCms.Presentation.Areas.DddCms.Models.UserViewModel;
using System;
using DddCms.ModelView.User;

namespace DddCms.Areas.Presentation.Controllers
{
    /// <summary>
    /// Represented the user interaction
    /// </summary>
    public class UserController : Controller
    {
        /// <summary>
        /// Gets or sets the user app.
        /// </summary>
        [Dependency]
        public IUserApp UserApp { get; set; }

        [Dependency]
        public ILoggingUtil LogWriter { get; set; }

        /// <summary>
        /// Register a user.
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Register()
        {
            return View(new RegisterModel());
        }

        [HttpPost]
        public ActionResult Register(RegisterModel registerModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    UserApp.CreateUser(registerModel.ToUserDto());
                    return View("Success");
                }
                catch (Exception ex)
                {
                    LogWriter.Information("Presentation", "User", ex.Message);
                }
            }

            return PartialView(registerModel);
        }

        //[HttpPost]
        //[Authorize()]
        //public ActionResult Register(bool inscriptionNewsLettre)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        try
        //        {
        //            UserApp.InscriptionNewsLettre(registerModel.ToUserDto());
        //            return View("Success");
        //        }
        //        catch (Exception ex)
        //        {
        //            LogWriter.Information("Presentation", "User", ex.Message);
        //        }
        //    }

        //    return PartialView("RegisterFormParial", registerModel);
        //}


        public JsonResult AlreadyExiste(string username)
        {
            if (UserApp.AlreadyExist(username))
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }

            return Json(true, JsonRequestBehavior.AllowGet);
        }

        /// <summary>-
        /// Logins this instance.
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Login()
        {
            return View(new LoginModel());
        }

        [HttpPost]
        public ActionResult Login(LoginModel loginModel)
        {
            LogWriter.Verbose("User", "Connection", "Tentative de connection");

            if (ModelState.IsValid)
            {
                if (UserApp.Validate(loginModel.UserName, loginModel.Password))
                    FormsAuthentication.SetAuthCookie(loginModel.UserName, loginModel.Souvenir);
                else
                {
                    ViewBag.ErrorMessage = "Login ou mot de passe incorrect";
                    return View(loginModel);
                }
            }
            else
                return View(loginModel);
           
            return RedirectToAction("UpdateProfile");
        }
        /// <summary>
        /// Updates the profile.
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult UpdateProfile()
        {
            ViewData["CurrentOpenID"] = "3";

            UpdateModel userModel = UserApp.getUser(HttpContext.User.Identity.Name).ToUpdateModel();
            return View(userModel);
        }
       
        /// <summary>
        /// Logouts this instance.
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Logout()
        {
            return View();
        }
    }
}
